'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:ChatUsersCtrl
 * @description
 * # ChatUsersCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('ChatUsersCtrl', function($routeParams, $location, ChatUser, RongYService, ChatStatus, DataService, openApi) {
        var vm = this;
        var targetId = $routeParams.id
        var init = function() {
            // vm.peoplelist = ChatUser.all();
            getDetail();
        }
        var getDetail = function() {
            var param = {}
            param.discussionId = targetId;
            RongYService.getDiscussion(param).then(function(data) {
                vm.peoplelist = ChatUser.getAll(data.memberIdList);
            })
        }
        if (ChatUser.isEmpty()) {
            DataService.getData(openApi.getuserinfo).then(function(data) {
                ChatUser.set(data);
                init();
            });
        } else {
            init();
        }
        this.goDetail = function(item) {
            $location.path("addressbook/userdetail/" + item.userId)
        }
        this.showMore = function() {
            vm.isShowMore = !vm.isShowMore;
            this.showdel = false
        };
        // 添加成员(会话类型由)
        this.addUsers = function() {
            ChatStatus.setTargetId(targetId);
            ChatStatus.setStatus(ChatStatus.UPDATE);
            $location.path("/chat/selectuser")
        };
        this.removeUsers = function() {
            this.showdel = !this.showdel
        }
        this.confirmdel = function(index) {
            if (navigator.notification) {
                navigator.notification.confirm(
                    '是否删除',
                    vm.checkConfirm(index),
                    '提示', ['删除', '取消']
                )
            }
        }
        this.checkConfirm = function(index) {
            var user = vm.peoplelist[index];
            return function(btn) {
                if (btn == 1) {
                    RongYService.removeMemberFromDiscussion(targetId, user.userId).then(function(data) {
                        vm.peoplelist.splice(index, 1);
                    })

                }
            }
        }
    });
